import java.util.*;

/**
 * Created with IntelliJ IDEA
 * Description  二叉树
 * User: 王杰
 * Date: 2025-06-03
 * Time: 17:29
 */
public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;// 存储左孩子的引用
        public TreeNode right;// 存储右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }

        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }

        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }

        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public static int nodeSize = 0;

    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
        return nodeSize;
    }

    public int size2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }

    // 获取叶子节点的个数
    public static int leafNodeCount = 0;

    public void getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            leafNodeCount++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }

    public int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }

        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }

        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }

    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
    }

    public static TreeNode findNode;

    public TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            findNode = root;
        }

        find(root.left, val);
        find(root.right, val);
        return null;
    }

//    public TreeNode find(TreeNode root, int val) {
//        if(root == null) {
//            return null;
//        }
//        if(root.val == val) {
//            return root;
//        }
//        TreeNode leftT = find(root.left, val);
//        if (leftT.val == val) {
//            return leftT;
//        }
//
//        TreeNode rightT = find(root.right, val);
//        if (rightT.val == val) {
//            return rightT;
//        }
//        return null;
//    }


    public static int i = 0;

    public static TreeNode createTree(String str) {
        TreeNode root = null;
        if (str.charAt(i) != '#') {
            root = new TreeNode(str.charAt(i));
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        } else {
            i++;
        }
        return root;
    }

    private static void inorderTree(TreeNode root) {
        if (root == null) {
            return;
        }
        inorderTree(root.left);
        System.out.print(root.val + " ");
        inorderTree(root.right);

    }

    public void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode cur = null;
        queue.offer(root);

        while (!queue.isEmpty()) {
            cur = queue.poll();
            System.out.print(cur.val + " ");

            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }

        }
    }


    // 层序遍历
    public List<List<Character>> levelOrder1(TreeNode root) {
        List<List<Character>> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode cur = null;
        queue.offer(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while (size != 0) {
                cur = queue.poll();
                list.add(cur.val);

                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }

    // 判断是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            return true;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            } else {
                break;
            }
        }

        while (!queue.isEmpty()) {

            if (queue.poll() != null) {
                return false;
            }
        }

        return true;
    }

    // 最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }

        if (root == p || root == q) {
            return root;
        }

        TreeNode leftTree = lowestCommonAncestor(root.left, p, q);
        TreeNode rightTree = lowestCommonAncestor(root.right, p, q);

        if (leftTree != null && rightTree != null) {
            return root;
        } else if (leftTree != null) {
            return leftTree;
        } else {
            return rightTree;
        }
    }

    /**
     * @param root
     * @param node  找的节点
     * @param stack 存储到栈中
     */
    public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if (root == null) {
            return false;
        }

        stack.push(root);

        if (root == node) {
            return true;
        }

        boolean ret = getPath(root.left, node, stack);
        if (ret == true) {
            return true;
        }
        ret = getPath(root.right, node, stack);
        if (ret == true) {
            return true;
        }
        stack.pop();
        return false;
    }

    public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }

        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();

        getPath(root, p, stackP);
        getPath(root, p, stackQ);

        int sizeP = stackP.size();
        int sizeQ = stackQ.size();

        if (sizeP > sizeQ) {
            int size = sizeP - sizeQ;
            while (size != 0) {
                stackP.pop();
                size--;
            }
        } else {
            int size = sizeQ - sizeP;
            while (size != 0) {
                stackQ.pop();
                size--;
            }
        }

        while (!stackP.isEmpty() && !stackQ.isEmpty()) {
            if (stackP.peek() == stackQ.peek()) {
                return stackP.peek();
            } else {
                stackQ.pop();
                stackP.pop();
            }
        }
        return null;
    }

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */

//    public int preIndex;
//
//    public TreeNode buildTree(int[] preorder, int[] inorder) {
//        return buildTreeChild(preorder, inorder, 0, inorder.length - 1);
//    }
//
//    public TreeNode buildTreeChild(int[] preorder, int[] inorder, int inbegin, int inend) {
//        if (inbegin > inend) {
//            return null;
//        }
//        TreeNode root = new TreeNode(preorder[preIndex]);
//
//        int rootIndex = findVal(inorder, inbegin, inend, preorder[preIndex]);
//
//        preIndex++;
//
//        root.left = buildTreeChild(preorder, inorder, inbegin, rootIndex - 1);
//        root.right = buildTreeChild(preorder, inorder, rootIndex + 1, inend);
//
//        return root;
//    }
//
//    private int findVal(int[] inorder, int inbegin, int inend, int val) {
//        for (int i = inbegin; i <= inend; i++) {
//            if (inorder[i] == val) {
//                return i;
//            }
//        }
//        return -1;
//    }

    // 中序非递归

//        public List<Integer> inorderTraversal(TreeNode root) {
//            List<Integer> list = new ArrayList<>();
//            if(root == null) {
//                return list;
//            }
//
//            Stack<TreeNode> stack = new Stack();
//            TreeNode cur = root;
//
//            while(cur != null || !stack.isEmpty()){
//                while(cur != null) {
//                    stack.push(cur);
//                    cur = cur.left;
//                }
//                TreeNode top = stack.pop();
//                list.add(top.val);
//                cur = top.right;
//            }
//            return list;
//        }

//    前序非递归
//    public List<Integer> preorderTraversal(TreeNode root) {
//        List<Integer> list = new ArrayList<>();
//        if (root == null) {
//            return list;
//        }
//
//        Stack<TreeNode> stack = new Stack();
//        TreeNode cur = root;
//
//        while (cur != null || !stack.isEmpty()) {
//            while (cur != null) {
//                stack.push(cur);
//                list.add(cur.val);
//
//                cur = cur.left;
//            }
//            TreeNode top = stack.pop();
//            cur = top.right;
//        }
//        return list;
//    }

//    后序非递归
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
//    class Solution {
//        public List<Integer> postorderTraversal(TreeNode root) {
//            List<Integer> list = new ArrayList<>();
//            if(root == null) {
//                return list;
//            }
//
//            Stack<TreeNode> stack = new Stack();
//            TreeNode cur = root;
//            TreeNode prev = null;
//
//            while(cur != null || !stack.isEmpty()){
//                while(cur != null) {
//                    stack.push(cur);
//                    cur = cur.left;
//                }
//                TreeNode top = stack.peek();
//                if(top.right == null || top.right == prev) {
//                    list.add(top.val);
//                    stack.pop();
//                    prev = top;
//                }else {
//                    cur = top.right;
//                }
//            }
//            return list;
//        }
//    }
}

